package com.surfilter.platform.base.model;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PageInfo extends ModelBase {
	private static final long serialVersionUID = -9045493664538088205L;

	private boolean dataPaged = false;
	private int start;
	private int limit;
	private int page;

	private boolean dataSort = false;
	private String sortName;
	private String sortType;

	private String multSortStr;
	
	public static PageInfo getPageInfo(HttpServletRequest request) {
		String pageStr = request.getParameter("page");
		String isPage = request.getParameter("isPage");
		String sortStr = request.getParameter("sort");
		PageInfo pageInfo = new PageInfo();
		pageInfo.dataPaged = false;
		
		if (sortStr != null && sortStr.trim().length() > 0) {
			pageInfo.dataSort = true;
			JSONArray array = JSONArray.fromObject(sortStr);
			//多条件排序
			pageInfo.multSortStr = parseArrayToOrderString(array) ;
			JSONObject obj = array.getJSONObject(0);
			pageInfo.sortName = obj.getString("property");
			pageInfo.sortType = obj.getString("direction");
		}
		if (pageStr != null && pageStr.trim().length() > 0 && isPage!=null && "true".equals(isPage.trim()) ) {
			pageInfo.dataPaged = true;
			pageInfo.page = Integer.parseInt(pageStr);

			String startStr = request.getParameter("start");
			String limitStr = request.getParameter("limit");
			pageInfo.start = Integer.parseInt(startStr);
			pageInfo.limit = Integer.parseInt(limitStr);
		}
		return pageInfo;
	}

	private static String parseArrayToOrderString(JSONArray array){
		Iterator itr = array.iterator();
		StringBuffer target = new StringBuffer();
		int i = 0;
		while(itr.hasNext()){
			JSONObject obj = (JSONObject)itr.next();
			String sortProperty = obj.getString("property");
			String sortDirection = obj.getString("direction");
			if(i == 0){
				target.append(sortProperty + " " + sortDirection);
			}else{
				target.append("," + sortProperty + " " + sortDirection);
			}
			i ++;
		}
		return target.toString().equals("") ? null : target.toString();
	}
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public boolean isDataPaged() {
		return dataPaged;
	}

	public void setDataPaged(boolean dataPaged) {
		this.dataPaged = dataPaged;
	}

	public boolean isDataSort() {
		return dataSort;
	}

	public void setDataSort(boolean dataSort) {
		this.dataSort = dataSort;
	}

	public String getSortName() {
		return sortName;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	public String getSortType() {
		return sortType;
	}

	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	public String getMultSortStr() {
		return multSortStr;
	}

	public void setMultSortStr(String multSortStr) {
		this.multSortStr = multSortStr;
	}
}
